"""
数据库迁移：为banner表添加link_appid字段
支持小程序跳转功能
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app import db, create_app
from sqlalchemy import text

def upgrade():
    """添加link_appid字段"""
    try:
        # 添加link_appid字段
        with db.engine.connect() as connection:
            connection.execute(text("""
                ALTER TABLE banners 
                ADD COLUMN link_appid VARCHAR(50) 
                COMMENT '小程序AppID（link_type为miniprogram时使用）'
            """))
            connection.commit()
        
        print("✓ 成功添加banner表的link_appid字段")
        
    except Exception as e:
        print(f"✗ 添加link_appid字段失败: {str(e)}")
        raise e

def downgrade():
    """删除link_appid字段"""
    try:
        # 删除link_appid字段
        with db.engine.connect() as connection:
            connection.execute(text("""
                ALTER TABLE banners 
                DROP COLUMN link_appid
            """))
            connection.commit()
        
        print("✓ 成功删除banner表的link_appid字段")
        
    except Exception as e:
        print(f"✗ 删除link_appid字段失败: {str(e)}")
        raise e

if __name__ == "__main__":
    app = create_app()
    with app.app_context():
        upgrade() 